<?php
/*
Simple:Press Forum
Related Tags
$LastChangedDate: 2009-04-22 02:36:11 -0700 (Wed, 22 Apr 2009) $
$Rev: 1757 $
*/

if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF']))
{
	die('Access Denied');
}

sf_forum_ahah_support();

include_once (SF_PLUGIN_DIR.'/forum/sf-forum-components.php');

global $current_user, $sfglobals, $sfvars;

sf_initialise_globals();

if(!isset($_GET['action'])) die();

$view = $_GET['action'];

# --------------------------------------------

$out='';
$out.= '<div id="sforum">'."\n";

# Display Header
$out.= '<div class="sfblock">'."\n";
if($view == 'watch')
{
	$out.= sf_render_main_header_table('forum', 0, __('Watched Topics', 'sforum'), __('List of topics that you are currently watching.', 'sforum'), SFRESOURCES.'watchicon.png');
} else {
	$out.= sf_render_main_header_table('forum', 0, __('Subscribed Topics', 'sforum'), __('List of topics to which you have subscribed.', 'sforum'), SFRESOURCES.'usersubscribed.png');
}

if($view == 'watch')
{
	$datarows = sf_get_watched_topics();
} else {
	$datarows = sf_get_subscribed_topics();
}

if ($datarows)
{
	$topics = $datarows['records'];
	$count = $datarows['count'];

	if ($topics)
	{
		# grab the complete first/last post stats
		$posts = array();
		foreach($topics as $topic)
		{
			$posts[$topic['topic_id']]=$topic['post_count'];
		}
		$stats = sf_get_combined_topic_stats($posts, $current_user->ID);

		# Start table display
		$out.= '<table class="sfforumtable">'."\n";

		# Column headers
		$out.= '<tr><th colspan="2"></th>'."\n";
		$out.= '<th>'.__("Started", "sforum").'</th>'."\n";
		$out.= '<th>'.__("Last Post", "sforum").'</th>'."\n";

		if($view == 'watch')
		{
			$out.= '<th>'.__("End Watch", "sforum").'</th>'."\n";
		} else {
			$out.= '<th>'.__("Unsubscribe", "sforum").'</th>'."\n";
		}
		$out.= '</tr>';

		$alt = '';

		foreach ($topics as $topic)
		{
			$forum = array();
			$forum['group_id'] = $topic['group_id'];
			$forum['forum_id'] = $topic['forum_id'];
			$forum['forum_slug'] = $topic['forum_slug'];
			$forum['forum_name'] = $topic['forum_name'];

			$value['forumlock'] = '';
			$value['watches'] = true;

			# Display current topic row

			$out.= '<tr id="topic-'.$topic['topic_id'].'">'."\n";
			$out.= '<td class="'.$alt.'">';
			$out.= '<table><tr><td class="sficoncell '.$alt.'">';
			$statkeys = sf_create_stats_keys($stats[$topic['topic_id']]);

			$out.= sf_render_topic_icon($topic['topic_id'], $stats[$topic['topic_id']][$statkeys[1]]['udate'], $stats[$topic['topic_id']]['thisuser'], $alt);
			$out.= '</td></tr>';
			$out.= '</table>';
			$out.= '</td>';
			$out.= '<td class="'.$alt.'">';
			$out.= '<p>';
			$forumname=sf_filter_title_display($forum['forum_name']);
			$out.=$forumname.'<br />';
			$out.= sf_render_topic_title($forum['forum_slug'], $topic['topic_slug'], sf_filter_title_display($topic['topic_name']))."\n";
			$out.= '</p>';

			if($sfglobals['display']['topics']['pagelinks'])
			{
				$out.= sf_render_inline_pagelinks($forum['forum_slug'], $topic['topic_slug'], $topic['post_count'])."\n";
			}
			# first/last post
			if(isset($statkeys[0]))
			{
				$out.= sf_render_first_last_post_cell($forum['forum_slug'], $topic['topic_slug'], $stats[$topic['topic_id']][$statkeys[0]], $alt);
				$out.= sf_render_first_last_post_cell($forum['forum_slug'], $topic['topic_slug'], $stats[$topic['topic_id']][$statkeys[1]], $alt);
			}

			if (!$sfglobals['lockdown'])
			{
				if($view == 'watch')
				{
					$out.= '<td align="center"><a href="'.sf_build_qurl('forum='.$forum['forum_id'], 'topic='.$topic['topic_id'],'endwatch=user').'"><img src="'.SFRESOURCES.'watchoff.png" alt="" title="'.esc_attr(__("End Topic Watch", "sforum")).'" />'.sf_render_icons("End Topic Watch").'</a></td>';
				} else {
					$out.= '<td align="center"><a href="'.sf_build_qurl('forum='.$forum['forum_id'], 'topic='.$topic['topic_id'],'endsub=user').'"><img src="'.SFRESOURCES.'unsubscribe.png" alt="" title="'.esc_attr(__("End Topic Subscription", "sforum")).'" />'.sf_render_icons("Unsubscribe").'</a></td>';
				}
			} else {
				$out.= '<td></td>';
			}

			$out.= '</tr>'."\n";

		}
		$out.= '</table>'."\n";
		$out.= '</div><br />'."\n";
	} else {
		if($view == 'watch')
		{
			$out.='<br /><div class="sfmessagestrip">'.__("You are not currently watching any topics!", "sforum").'</div>'."\n";
		} else {
			$out.='<br /><div class="sfmessagestrip">'.__("You are not currently subscribed to any topics!", "sforum").'</div>'."\n";
		}
		$out.='</div>'."\n";
	}
} else {
	if($view == 'watch')
	{
		$out.='<br /><div class="sfmessagestrip">'.__("You are not currently watching any topics!", "sforum").'</div>'."\n";
	} else {
		$out.='<br /><div class="sfmessagestrip">'.__("You are not currently subscribed to any topics!", "sforum").'</div>'."\n";
	}
	$out.='</div>'."\n";
}

$out.= '</div>'."\n";

echo $out;

die();

?>